<?
class User
{
	public function __construct($rs = null)
	{
		if($rs != null)
		{
			$this->id = isset($rs['id']) ? $rs['id'] : null;
			$this->FirstName = $rs['FirstName'];
			$this->LastName = $rs['LastName'];
			$this->Birthday = $rs['Birthday'];
		}
	}
	
	public $id;
	public $FirstName;
	public $LastName;
	public $Birthday;
	//***************************************************	
	static function Get($id)
	{
		$conn = GetConnection();
		$results = $conn->query("SELECT * FROM Users WHERE id=$id");
		if($rs = $results->fetch_assoc())
		{
			$conn->close();
			return $rs;
		}
		$conn->close();
		return null;
	}
	//***************************************************	
	static function GetAll()
	{
		$conn = GetConnection();
		return $conn->query("SELECT * FROM Users");
	}
	//***************************************************	
	static function Delete($id)
	{
		$conn = GetConnection();
		$sql = "DELETE FROM Users WHERE id=$id";
		$conn->query( $sql );
		$error = $conn->error;	
		$conn->close();
		
		if( $error != '' ) throw new Exception("Error:\nsql=$sql\nerror message\n$error");
	}
	//***************************************************	
	static function Validate($rs)
	{
		$returnValue = null;
		
		if(!isset($rs['FirstName']) || $rs['FirstName'] == '')
			$returnValue['FirstName'] = 'Is Required';
		if(!isset($rs['LastName']) || $rs['LastName'] == '')
			$returnValue['LastName'] = 'Is Required';
		if(count($rs['LastName']) > 50)
			$returnValue['LastName'] = 'Maximum size is 50 charechters';
		if(!isset($rs['Birthday']) || $rs['Birthday'] == '')
			$returnValue['Birthday'] = 'Is Required';
		$date = strtotime($rs['Birthday']);
		if($date == null)
			$returnValue['Birthday'] = 'Must be a valid Date';
		return $returnValue;
	}
	//***************************************************	
	static function Create($rs)
	{
		$conn = GetConnection();
		$rsClean = escapeAllFields($rs, $conn);
		$birthday = date('Y-m-d', strtotime( $rsClean['Birthday'] ));
		
		$sql = "Insert Into Users (FirstName, LastName, Birthday) Values ("
			. "'$rsClean[FirstName]', '$rsClean[LastName]', '$birthday' )";
		$conn->query( $sql );
		$error = $conn->error;	
		$conn->close();
		
		if( $error != '' ) throw new Exception("Error:\nsql=$sql\nerror message\n$error");
	}
	//***************************************************	
	static function Edit($rs)
	{
		$conn = GetConnection();
		$rsClean = escapeAllFields($rs, $conn);
		$birthday = date('Y-m-d', strtotime( $rsClean['Birthday'] ));
	
		$sql = "Update Users SET "
			. "FirstName = '$rsClean[FirstName]', "
			. "LastName = '$rsClean[LastName]', "
			. "Birthday = '$birthday' "
			. "WHERE id=$rsClean[id] ";
		$conn->query( $sql );
		$error = $conn->error;	
		$conn->close();
		
		if( $error != '' ) throw new Exception("Error:\nsql=$sql\nerror message\n$error");
	}
	//***************************************************
	static function GetEmail($rs){
		$conn = GetConnection();
		$sql = "SELECT * FROM Users WHERE FirstName = '$rs[FirstName], LastName = '$rs[LastName]'";
		$lastName = $conn->query($sql);
		if($lastName == null){
			return $error = $conn->error;
			$conn->close();
		}else{
			$conn->close();
			return '$sql[Email]';
		}
	}
}
